package hr.fer.pus.dll_will.cr.actions;

import hr.fer.pus.dll_will.cr.IActionHandler;
import hr.fer.pus.dll_will.sp.SPDescriptor;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

public class ListSPs implements IActionHandler {

	private List<SPDescriptor> sps;
	
	public ListSPs(List<SPDescriptor> sps){
		this.sps = sps;
	}
	
	@Override
	public boolean performAction(ServletOutputStream out,
			HttpServletRequest request) throws IOException {
		
		if (sps == null){
			// silent fail?
			return false;
		}
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < sps.size(); ++i){
			String sp = URLEncoder.encode(sps.get(i).toString(), "UTF-8");
			sb.append(sp);
			if((i+1) != sps.size()) sb.append("#");
		}
		String encoded = URLEncoder.encode(sb.toString(), "UTF-8");
		out.write(encoded.getBytes());
		return true;
	}

}
